<template>
  <div>
    <RadioGroup v-model="border">
      
      <Radio label="TS">
        <Icon type="logo-chrome" />
        <span>JSON转TS接口</span>
      </Radio>
      <Tooltip content="请使用基本JS语法">
        <Radio label="JSON" disabled>
          <Icon type="md-battery-full" />
          <span>JS对象转JSON</span>
        </Radio>
      </Tooltip>
    </RadioGroup>
    <Tooltip content="进行处理，将结果输出在右侧输入框">
      <Button type="primary" @click="parse">处理</Button>
    </Tooltip>
    <Row>
      <Col span="12">
        <Input
          v-model="input"
          type="textarea"
          :autosize="{ minRows: 60, maxRows: 80 }"
          placeholder="输入要转换的数据"
        />
      </Col>
      <Col span="12">
        <Input
          v-model="output"
          type="textarea"
          :autosize="{ minRows: 60, maxRows: 80 }"
          placeholder="结果..."
          readonly
        />
      </Col>
    </Row>
  </div>
</template>

<script>
import common from "../../lib/common";
import fs from "fs";
import * as CONSTANT from "../../lib/constants";
import json2ts from "json2ts";
import { EOL } from "os";
export default {
  data() {
    return {
      input: "",
      output: "",
      border: "TS",
    };
  },
  methods: {
    parse() {
      if (!this.input) {
        this.$Message.error("请输入需要转换的内容");
        return;
      }
      if (this.border == "JSON") {
        this.JS2JSON();
      } else if (this.border == "TS") {
        try {
            JSON.parse(this.input);
        } catch (error) {
            this.$Message.error(error.toString());
            return;
        }
        let res = json2ts.convert(this.input);
        console.log("res :>> ", res);
        this.output = res;
      } else {
        this.$Message.error("未知错误");
      }
    },

    //JS对象转JSON
    JS2JSON() {
      let res = common.js2Json(this.input);
      this.output = res;
    },
  },
  created() {
  },
};
</script>

<style>
</style>